/*
  Copyright 2011 fullkawa

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
 */
package jp.fullkawa.xnovel.content;

import jp.fullkawa.xnovel.exception.RequireException;

/**
 * 変数のセット
 * @author fullkawa
 */
public class Let extends Content
{
	/**
	 * 本に対して保存される。ゲーム進捗などに向いている。
	 */
	public final static String PREFIX_SYS = "sys:";

	/**
	 * しおりに対して保存される。しおりが違うと別の変数と見なす。
	 */
	public final static String PREFIX_SAVE = "save:";

	/**
	 * 雑用、本を開いて閉じると無くなります。
	 */
	public final static String PREFIX_TMP = "tmp:";

	/**
	 * 前置詞
	 */
	private String prefix;

	/**
	 * 変数名。ただし、前置詞は除く
	 */
	private String name;

	/**
	 * 値
	 */
	private String text;

	/**
	 * この値がtrueのとき、値に演算子が含まれていれば演算される
	 */
	private boolean calc = true;

	public Let() {}

	public Let(String prefix, String name, String text)
	{
		this();
		this.setPrefix(prefix);
		this.setName(name);
		this.setText(text);
	}

	public Let(String prefix, String name, String text, boolean calc)
	{
		this(prefix, name, text);
		this.setCalc(calc);
	}

	public String getPrefix() {
		return prefix;
	}

	public void setPrefix(String prefix) {
		this.prefix = prefix;
	}

	/**
	 * @return 前置詞(prefix)なしの名前
	 */
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getText() {
		return text;
	}

	public void setText(String text) {
		this.text = text;
	}

	public boolean isCalc() {
		return calc;
	}

	public void setCalc(boolean calc) {
		this.calc = calc;
	}

	/**
	 * @return 前置詞(prefix)付きの名前
	 */
	public String getCanonicalName() {
		String cname = this.getPrefix() + this.getName();
		return cname;
	}

	@Override
	public String getTag()
	{
		if (this.getCanonicalName() == null)
		{
			throw new RequireException("変数名が設定されていません。");
		}
		String text = "";
		if (this.getText() != null)
		{
			text = this.getText();
		}

		Object[] o = { this.getCanonicalName(), text };
		return super.getTag("tag.let", o);
	}
}
